package com.heima.user.service.impl;
import java.util.Date;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dtos.AppHttpCodeEnum;
import com.heima.common.dtos.PageResponseResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.user.dtos.UserManagerDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserManagerService;
import com.heima.utils.common.BeanHelper;
import com.heima.wemedia.feign.WemediaFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ApUserManagerServiceImpl implements ApUserManagerService {

    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;

    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private WemediaFeign wemediaFeign;

    @Override
    public ResponseResult<List<ApUserRealname>> list(UserManagerDto dto) {
        //校验参数
        dto.checkParam();

        //创建分页对象
        IPage<ApUserRealname> ipage = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();

        //如果状态参数不为空，则根据状态查询

        if (dto.getStatus()!=null){
            wrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        }

        ipage = apUserRealnameMapper.selectPage(ipage, wrapper);

        //封装返回结果
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)ipage.getTotal());
        pageResponseResult.setData(ipage.getRecords());
        return pageResponseResult;
    }

    @Override
    @Transactional
    public ResponseResult authFail(UserManagerDto dto) {
        //参数校验
        if (dto.getId()==null){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //根据id查询用户信息
        ApUserRealname apUserRealname = apUserRealnameMapper.selectById(dto.getId());

        //数据不存在
        if (apUserRealname==null){
            throw new LeadNewsException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //设置状态和原因
        apUserRealname.setStatus((short) 2);
        apUserRealname.setReason(dto.getMsg());

        //修改用户状态
        apUserRealnameMapper.updateById(apUserRealname);

        return ResponseResult.okResult(null);
    }

    @Override
    @Transactional
    public ResponseResult authPass(UserManagerDto dto) {
        //参数校验
        if (dto.getId()==null){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //根据id查询用户信息
        ApUserRealname apUserRealname = apUserRealnameMapper.selectById(dto.getId());

        //数据不存在
        if (apUserRealname==null){
            throw new LeadNewsException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //设置状态和原因
        apUserRealname.setStatus((short) 9);

        //修改用户状态
        apUserRealnameMapper.updateById(apUserRealname);

        //根据APP用户id查询APP用户
        ApUser apUser = apUserMapper.selectById(apUserRealname.getUserId());
        if (apUser!=null){
            WmUser wmUser = new WmUser();
            wmUser.setPassword(apUser.getPassword());
            wmUser.setName(apUser.getName());
            wmUser.setApUserId(apUser.getId());
            wmUser.setPhone(apUser.getPhone());
            wmUser.setStatus(1);
            wmUser.setCreatedTime(new Date());

            ResponseResult responseResult = wemediaFeign.saveWmUser(wmUser);
            if (responseResult.getCode().equals(200)){
                Object data = responseResult.getData();
                System.out.println(data);
            }
        }


        return ResponseResult.okResult(null);
    }
}
